<template>
  <div>
    <el-dialog
      title="运费设置"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :show-close="true"
      :close-on-press-escape="false"
    >
      <el-form
        :model="orderCarry"
        :rules="rules"
        label-position="right"
        label-width="100px"
        ref="form"
      >
        <el-form-item label="价格" prop="fee">
          <el-input v-model="orderCarry.fee" type="number" />
        </el-form-item>
        <el-form-item label="地区" prop="region">
          <el-select
            v-model="orderCarry.region"
            multiple
            filterable
            allow-create
            default-first-option
            placeholder="请选择地区"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmClick">确 定</el-button>
        <el-button @click="cancelClick">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  // id即编辑的id，visible属性用来监听是否显示弹窗
  props: ["visible"],
  components: {},
  watch: {},
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(value) {
        this.$emit("update:visible", value);
      }
    }
  },
  data() {
    return {
      // 弹窗相关属性
      dialogFieldBox: false,
      options: [],
      orderCarry: {
        id: null,
        fee: 0,
        region: []
      },
      // 表单校验
      rules: {
        fee: [
          {
            required: true,
            message: "费用不能为空",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    initOrderCarry() {
      this.$https.get("product/getOrderCarry", {}).then(res => {
        let carry = res.data.data;
        if (carry) {
          this.orderCarry.id = carry.id;
          this.orderCarry.fee = carry.fee;
          this.orderCarry.region = carry.region.split(",");
          this.options = this.orderCarry.region;
        }
      });
    },
    // 确认保存
    confirmClick() {
      let param = { ...this.orderCarry };
      param.region = this.orderCarry.region.join(",");
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$https.postXform("product/orderCarry", param).then(res => {
            this.$message({ message: "保存成功", type: "success" });
            this.dialogVisible = false;
            this.$emit("success");
          });
        } else {
          return false;
        }
      });
    },
    cancelClick() {
      this.dialogVisible = false;
    }
  },
  mounted() {},
  created() {}
};
</script>

<style lang="less">
</style>
